
(function() {

(function () {
	chrome.storage.local.get('settings', function (args) {
		if (args.settings.stopAutoPlay) {
			unplayLiveStream();
		}
	})
})();

function getReplacement(id, width, height) {
	return $('<div id="doPlay_' + id + '" style="position:relative;width:' + width +
			'px;height:' + height + 'px;overflow:hidden;text-align:center;line-height:' + height + 'px;">' +
				'<img src="/images/flowplayer/fpPlayButtonBig.png" style="cursor:pointer" alt="Click to play live stream">' + 
				'<div style="line-height:1em;margin-top:-' + (height / 4) + 'px;direction:ltr">' +
					'Click to start live stream' +
					'<br/><br/>' +
					'<small style="color:gray">' +
						'Quiet, Ynet. QuieNet.' +
					'</small>' +
				'</div>' +
			'</div>');
}

function replaceLiveStream(container) {
	var id = container.attr('id');
	var play = getReplacement(id, container.width(), container.height());
	var orgContainer = container.replaceWith(play);
	play.click(function() {
		$('#doPlay_' + id).replaceWith(orgContainer);
	});
}

//TODO find a better way to replace stream player, preferably leaving the image placeholder
function unplayLiveStream() {
	$('[id^=article_live_tv-] iframe').each( function() {
		var container = $(this);
		replaceLiveStream(container);
	});
}

function init() {
	chrome.storage.local.set({'settings': {'stopAutoPlay': true}}, function (){});
}

})();

// live streaming: http://www.ynet.co.il/articles/0,7340,L-4546067,00.html
// multiple players: http://www.ynet.co.il/articles/0,7340,L-4546435,00.html
// no autoplay: http://www.ynet.co.il/articles/0,7340,L-4546304,00.html
// non-first autoplayer: http://www.ynet.co.il/articles/0,7340,L-4545001,00.html